package cn.stylefeng.guns.modular.system.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.entity.IndentOrder;
import cn.stylefeng.guns.modular.system.mapper.IndentOrderMapper;
import cn.stylefeng.guns.modular.system.model.params.IndentOrderParam;
import cn.stylefeng.guns.modular.system.model.result.IndentOrderResult;
import  cn.stylefeng.guns.modular.system.service.IndentOrderService;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订货单 服务实现类
 * </p>
 *
 * @author 小飞
 * @since 2019-07-02
 */
@Service
public class IndentOrderServiceImpl extends ServiceImpl<IndentOrderMapper, IndentOrder> implements IndentOrderService {
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private IdentityService identityService;
    @Resource
    private TaskService taskService;

    @Override
    public IndentOrderResult getByProcessInstanceId(String processInstanceId) {
        return this.baseMapper.getByProcessInstanceId(processInstanceId);
    }

    @Override
    public void add(IndentOrderParam param){
        IndentOrder entity = getEntity(param);
        identityService.setAuthenticatedUserId(ShiroKit.getUserNotNull().getId() + "");
        //启动流程实例，字符串"vacation"是BPMN模型文件里process元素的id
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("indent_order");
        //流程实例启动后，流程会跳转到经销商提交
        Task vacationApply = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        //设置经销商提交任务的执行人
        taskService.setAssignee(vacationApply.getId(), ShiroKit.getUserNotNull().getId() + "");



        this.save(entity);
    }

    @Override
    public void delete(IndentOrderParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(IndentOrderParam param){
        IndentOrder oldEntity = getOldEntity(param);
        IndentOrder newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public IndentOrderResult findBySpec(IndentOrderParam param){
        return null;
    }

    @Override
    public List<IndentOrderResult> findListBySpec(IndentOrderParam param){
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(IndentOrderParam param,Long id){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,id);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(IndentOrderParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private IndentOrder getOldEntity(IndentOrderParam param) {
        return this.getById(getKey(param));
    }

    private IndentOrder getEntity(IndentOrderParam param) {
        IndentOrder entity = new IndentOrder();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
